from bs4 import BeautifulSoup #
from selenium import webdriver #浏览器
import time #时间操作
import json

#爬取dgsx stu 信息
from com.py.test.discern.ReptleStuMsg import ReptleStuMsg
from com.py.test.discern.disscemPhoto import disscem
from selenium.webdriver.common.keys import Keys


class ReptileStuDgsx:
    def __init__(self):
        self.url = "http://dgsx.gdcp.cn/"
        self.basePath = "D://dgsxStu"
    def start(self,loginName):
        print("——开始自动化爬取———")
        #浏览器设置
        options = webdriver.ChromeOptions()
        #设置中文
        options.add_argument('lang=zh_CN.UTF-8')
        #更换头部
        ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'
        options.add_argument('user-agent="%s"' % ua)
        driver = webdriver.Chrome(chrome_options=options)
        driver.get(self.url)
        # self.scroll_win(driver,1)  #下拉刷新 1 次数
        assert "顶岗实习" in driver.title
        time.sleep(4)
        #切换iframe
        # driver.switch_to_frame("iframe_all")
        print("开始输入")
        #输入账号
        # loginName = "1613160225" #clq
        # loginName = "1613160228" #谢博智 无岗位信息
        elem = driver.find_element_by_name("loginName")  #找到name为loginName的元素，账号框
        elem.clear()  #清空搜索框中的内容
        elem.send_keys(loginName)  ##在框中输入账号
        #输入密码
        elem = driver.find_element_by_name("password")  #密码框
        elem.clear()  #清空搜索框中的内容
        elem.send_keys(loginName[4:])  #在框中输入密码
        #输入验证码
        #验证码地址如下
        codeUrl = BeautifulSoup(driver.page_source,'lxml').find('img',id="imgObj")
        codeUrl = self.url+codeUrl['src']
        print("验证码是:",codeUrl)
        #保存cookie
        cookies = driver.get_cookies()
        with open("cookies.txt", "w") as fp:
            json.dump(cookies, fp)
        #设置验证码
        self.setCode(driver,codeUrl)

        #开始爬取
        rs = ReptleStuMsg()
        rs.start(driver, codeUrl, self)

        driver.quit()
    def setCode(self,driver,codeUrl):
        #识别验证码
        ds = disscem();
        text = ds.loadImage(codeUrl)
        print("验证码是:",text)
        elem = driver.find_element_by_name("veryCode")  #密码框
        elem.clear()  #清空搜索框中的内容
        elem.send_keys(text)  #在框中输入验证码
        elem.send_keys(Keys.RETURN)  #相当于回车键，提交
for x in range(1,4):
    for i in range(1 ,55):
        stuNo ="1732138"+str(x)
        if len(str(i)) == 1:
            stuNo = stuNo+"0"+str(i)
        else:
            stuNo = stuNo+str(i)
        print(stuNo)
        rp =ReptileStuDgsx();
        rp.start(stuNo)


#selenium 多开窗口
# def newOpen():
    # 新开一个窗口，通过执行js来新开一个窗口
    # js = 'window.open("'+codeUrl+'");'
    # driver.execute_script(js)
    # # 输出当前窗口句柄
    # baidu_handle = driver.current_window_handle
    # # 获取当前窗口句柄集合（列表类型）
    # handles = driver.window_handles
    # print(handles)  # 输出句柄集合
    # # ['CDwindow-E9B85270B51D42AF7369D81B9AA70FFE',
    # # 'CDwindow-90004FD79A0F59EE057846B34D0E7327']
    # # 获取搜狗窗口
    # sogou_handle = None
    # for handle in handles:
    #     if handle != baidu_handle:
    #         sogou_handle = handle
    # # 输出当前窗口句柄（搜狗）
    # print('switch to ', handle)
    # driver.switch_to.window(baidu_handle)